package org.biojava.services.das.servlets;

import javax.servlet.http.HttpServlet;

import java.util.logging.Logger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;

import uk.ac.sanger.dasregistry.timer.Archiver;
import uk.ac.sanger.dasregistry.timer.AutoValidationWrapper;
import uk.ac.sanger.dasregistry.timer.CleanLogs;
import uk.ac.sanger.dasregistry.timer.CleanRegistryWrapper;

import uk.ac.sanger.dasregistry.timer.MirroringWrapper;

/**
 * Servlet implementation class RegistrySchedulerServlet
 */
public class RegistrySchedulerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Scheduler scheduler;
	public static Logger log=Logger.getLogger(" org.biojava.services.das.servlets");
	JobDetail cleanServersJob;

	JobDetail validationJob;
	
	JobDetail mirroringJob;

	JobDetail archiving;
	
	JobDetail cleanLogsJob;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegistrySchedulerServlet() {
		super();
			 
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();// default
																	// scheduler
																	// has been
																	// specified
																	// as this
																	// class in
																	// quartz.properties

			// and start it off
			scheduler.start();

			// Define job instance
			//cleanServersJob = new JobDetail("servers", "group1",
					//CleanRegistryWrapper.class);

			validationJob = new JobDetail("autovalidation", "group1",
					AutoValidationWrapper.class);
			log.info("autovalidation scheduled");
			//mirroringJob = new JobDetail("mirroring", "group1",
			//		MirroringWrapper.class);
			//log.info("mirrroring scheduled");
			archiving = new JobDetail("archiving", "group1",
					Archiver.class);
			log.info("archiving scheduled");
			cleanLogsJob = new JobDetail("cleanLogs", "group1",
					CleanLogs.class);
			log.info("clean logs scheduled");
			log.info("scheduler setup");
			//Trigger serversTrigger = TriggerUtils.makeHourlyTrigger("servers",
					//24, 1000000000);
//			Trigger indexTrigger = TriggerUtils.makeDailyTrigger("indexer",
//					16, 30);//1.30am each day
			
			// will go every 10 hours if set as only one machine in our cluster
			// is allowed to run indexing
			// comment out this servlet from develoment web.xml documents to
			// prevent sending emails to some people
			Trigger validationTrigger = TriggerUtils.makeHourlyTrigger(
					"autovalidation", 6, 1000000000);
			// run every three hours or so - multiple machines can do this but
			// only if last one longer than 3 hours ago (checked by DAO Admin
			// checks that looks in database)
			//Trigger mirroringTrigger = TriggerUtils.makeWeeklyTrigger(
			//		"mirroring", 1, 1, 1);// run on Sunday at 1;01am
			// jobs will be staggered as lastValidation etc times will be
			// different.
			Trigger archivingTrigger = TriggerUtils.makeDailyTrigger(
					"archiving", 4, 1); // 4:01am
			// Trigger archivingTrigger =
			// TriggerUtils.makeMinutelyTrigger("archiving", 4, 10); //4:01am
			//Trigger cleanLogsTrigger=TriggerUtils.makeMinutelyTrigger("cleanLogs", 1,1000000);
			Trigger cleanLogsTrigger=TriggerUtils.makeDailyTrigger("cleanLogs", 0,0);
			// Schedule the job with the trigger
			//scheduler.scheduleJob(cleanServersJob, serversTrigger);
			//scheduler.scheduleJob(indexerJob, indexTrigger);
			scheduler.scheduleJob(validationJob, validationTrigger);
			//scheduler.scheduleJob(mirroringJob, mirroringTrigger);
			scheduler.scheduleJob(archiving, archivingTrigger);
			scheduler.scheduleJob(cleanLogsJob, cleanLogsTrigger);

		} catch (SchedulerException se) {
			se.printStackTrace();
		}
		
	}

	
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		try {
			System.out.println("calling destroy on scheduler servlet");
//			scheduler.deleteJob("servers", "group1");
//			log.info("servers thread deleted");
//			scheduler.deleteJob("autovalidation", "group1");
//			log.info("autovalidation deleted");
//			scheduler.deleteJob("mirroring", "group1");
//			log.info("mirrroring deleted");
//			scheduler.deleteJob("archiving", "gropu1");
//			log.info("archiving deleted");
//			scheduler.deleteJob("cleanLogs", "group1");
			scheduler.shutdown();
			System.out.println("shutting down scheduler");
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
